package com.deductive.web.disposition.special;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.core.page.TableDataInfo;
import com.deductive.common.utils.DateUtils;
import com.deductive.domain.layer.Models3d;
import com.deductive.domain.special.SpatialShip;
import com.deductive.service.layer.Models3dService;
import com.deductive.service.special.SpatialShipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("specialShip")
@Api(tags = "舰艇管理")
public class SpatialShipController  extends BaseController {


    @Resource
    private SpatialShipService spatialShipService;

    @Resource
    private Models3dService models3dService;

    @GetMapping("/pageQuery")
    @ApiOperation("分页查询")
    public TableDataInfo selectAll(SpatialShip spatialShip) {
        startPage();
        LambdaQueryWrapper<SpatialShip> eq = new LambdaQueryWrapper<>();
        eq.like(StringUtils.isNotBlank(spatialShip.getName()),SpatialShip::getName,spatialShip.getName());
        List<SpatialShip> list = this.spatialShipService.list(eq);
        for (SpatialShip ship : list) {
            Long model3DId = ship.getModel3DId();
            if (model3DId!=null){
                Models3d byId = models3dService.getById(model3DId);
                String url = byId.getUrl();
                ship.setModelUrl(url);
            }
        }
        return getDataTable(list);
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    public AjaxResult selectOne(@PathVariable Integer id) {
        return success(this.spatialShipService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增数据")
    public AjaxResult insert(@RequestBody SpatialShip spatialShip) {
        spatialShip.setCreateTime(DateUtils.getNowDate());
        return success(this.spatialShipService.save(spatialShip));
    }

    @PutMapping
    @ApiOperation("修改数据")
    public AjaxResult update(@RequestBody SpatialShip spatialShip) {
        spatialShip.setUpdateTime(DateUtils.getNowDate());
        return success(this.spatialShipService.updateById(spatialShip));
    }

    @DeleteMapping
    @ApiOperation("删除数据")
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.spatialShipService.removeByIds(idList));
    }
}
